﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuickManager.Code
{
    public class DB
    {
        public static string DBPath = AppDomain.CurrentDomain.BaseDirectory + "/Data/data.db";
        static DB()
        {

            if (File.Exists(DBPath) == false)
            {
                SQLiteConnection.CreateFile(DBPath);
            }

            using (SQLiteConnection conn = DB.GetConnection())
            {
                conn.Open();
                SQLiteCommand cmd = new SQLiteCommand(conn);

                //全局配置表
                cmd.CommandText = @"create table if not exists ConfigInfo(
ConfigName text primary key, 
ConfigValue text
)";
                cmd.ExecuteNonQuery();

                //站点分类表
                cmd.CommandText = @"create table if not exists GroupInfo(
GroupID text primary key, 
GroupName text,
GroupType text
)";
                cmd.ExecuteNonQuery();

                //站点表
                cmd.CommandText = @"create table if not exists SiteInfo(
SiteID text primary key, 
GroupID text,
SiteName text,
LocalPath text,
Port text,
NetVersion text,
PoolType text,
RegisterName text,
SourcePath text,
SlnFile text
)";
                cmd.ExecuteNonQuery();

                //需求文档挂接站点表
                cmd.CommandText = @"create table if not exists LinkRequire(
LinkID text primary key, 
SiteID text,
RequireCode text
)";
                cmd.ExecuteNonQuery();
            }

        }
        public static SQLiteConnection GetConnection()
        {
            SQLiteConnection conn = new SQLiteConnection(string.Format("Data Source={0}", DBPath));
            return conn;
        }

        public static SQLiteDataContext GetContext()
        {
            SQLiteDataContext context = new SQLiteDataContext(string.Format("Data Source={0}", DBPath));
            return context;
        }
    }
}
